package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import cn.sumpu.app.guoguojie.biz.CommentBiz;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.biz.MemberBiz;
import cn.sumpu.app.guoguojie.biz.MentionBiz;
import cn.sumpu.app.guoguojie.biz.MessageBiz;
import cn.sumpu.app.guoguojie.dao.CommentDao;
import cn.sumpu.app.guoguojie.domain.Comment;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.PageModel;

public class CommentBizImpl implements CommentBiz {
    private CommentDao    commentDao;
    @Autowired
    private MentionBiz    mentionBiz;
    @Autowired
    private MemberBiz     memberBiz;
    @Resource
    private IdentifierBiz identifierBiz;

    public void insert(Comment comment) throws AppBusinessException {
        commentDao.insert(comment);
        //		评论未读信息加1
        //messageBiz.messageAmount(comment.getToFamily(), MessageAmountType.COMMENT.getValue());
    }

    public void setCommentDao(CommentDao commentDao) {
        this.commentDao = commentDao;
    }

    @Override
    public void updateComment(Comment comment) throws AppBusinessException {
        commentDao.updateComment(comment);

    }

    @Override
    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    public void deleteCommentById(String id) throws AppBusinessException {
        commentDao.deleteCommentById(id);
        //删除评论的时候也删除提到我家的
        mentionBiz.deleteMentionById(id);
    }

    @Override
    public Comment getCommentById(String id) throws AppBusinessException {
        return commentDao.getCommentById(id);
    }

    @Override
    public int getCommentCountList(String commentId, String commentIdType) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("commentId", commentId);
        map.put("commentIdType", commentIdType);
        return commentDao.getCommentCountList(map);
    }

    @Override
    public int getCommentByOwnerTypeCount(Map<String, Object> map) throws AppBusinessException {
        return commentDao.getCommentByOwnerTypeCount(map);
    }


    //根据BlogId批量屏蔽评论
    @Override
    public void updateByCommentIdList(String idList, Boolean enabled) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", idList);
        map.put("enabled", enabled);
        commentDao.updateByCommentIdList(map);
        //屏蔽评论的同时屏蔽提到我家的
        mentionBiz.updateMentionByCondition(enabled, idList, null);
    }

    //根据BlogId批量删除评论
    @Override
    public void deleteByCommentIdList(String idList) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", idList);
        commentDao.deleteByCommentIdList(map);
    }

    //根据条件批量删除评论(参数1.id:commentId 2.type:comment_id_type[可选])
    @Override
    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    public void deleteCommentByCondition(String id, String type) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("type", type);
        commentDao.deleteCommentByCondition(map);
        //删除评论的同时删除提到我家的
        mentionBiz.deleteMentionByCondition(id, null);
    }

    //评论，对回复的评论
    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    public String insertCommentOrReply(Comment comment) throws AppBusinessException {
        String replyCommentId = comment.getReplyCommentId();
        if (Common.notEmptyStr(replyCommentId)) {
            Comment cmt = commentDao.getCommentById(replyCommentId);
            Member m = memberBiz.getMemberById(cmt.getMember());
            String content = comment.getContent();
            content = content + "//@" + m.getNickname() + ":" + cmt.getContent();
            comment.setContent(content);
        }
        comment.setIdentifier(identifierBiz.generateId("comment"));
        commentDao.insert(comment);
        //		评论未读信息加1
       // messageBiz.messageAmount(comment.getToFamily(), MessageAmountType.COMMENT.getValue());
        return comment.getContent();
    }

    @Override
    public List<Comment> getCommentByDoingId(String doingId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startId", 0);
        map.put("pageSize", Common.PAGE_SIZE_DOING_COMMENT);
        map.put("doingId", doingId);
        return commentDao.getCommentByDoingId(map);
    }

    @Override
    public void deleteCommentByCommentId(String commentId) throws AppBusinessException {
        commentDao.deleteCommentByCommentId(commentId);

    }

    @Override
    public PageModel getCommentListByDoingId(String doingId, Integer page) throws AppBusinessException {
        if(page == null || page < 1){
            page = 1;
        }
        int pageSize = Common.PAGE_SIZE_DOING_COMMENT;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("doingId", doingId);
        int count = commentDao.getCommentByDoingIdCount(map);
//        map.put("startId", (page - 1) * pageSize);
//        map.put("pageSize", pageSize);
        List<Comment> commentList = commentDao.getCommentByDoingId(map);

        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        pm.setList(commentList);
        pm.setPageSize(pageSize);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + pageSize - 1) / pageSize);
        return pm;
    }

    @Override
    public int getMyComCount(String family) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("family", family);
        return commentDao.getMyComCount(map);
    }

    /**
     * 
     * @param discussId
     * @param page
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.CommentBiz#getCommentListByDoingId(java.lang.String, java.lang.Integer)
     */
    @Override
    public PageModel getCommentListByDiscuss(String id, String type,Integer page) throws AppBusinessException { 
        if (Common.empty(page)) {
            page = 1;
        } 
        int pageSize = Common.PAGE_SIZE_DISCUSS_COMMENT;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("commentId", id);
        map.put("commentIdType", type);
        int count = commentDao.getCountByDiscuss(map);
        map.put("startId", (page - 1) * pageSize);
        map.put("pageSize", pageSize); 
        List<Comment> commentList = commentDao.getCommentByDiscuss(map);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        pm.setList(commentList);
        pm.setPageSize(pageSize);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + pageSize - 1) / pageSize);
        return pm;
    }

}
